<?php
/**
 * (jobs) 国家备货品类/sku预估
 */
namespace App\Jobs\Stocking;

use App\Models\Basics\SysArea;
use App\Models\Product\ProductSku;
use App\Models\SsBasic\CountryClassifyEstimate;
use App\Models\SsBasic\CountrySkuEstimate;
use App\Models\SsBasic\CountryStockingSku;
use App\Models\SsBasic\CountryStockingSkuRatio;
use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
class CountrySkuRatioJob implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    protected $ratio;

    /**
     * SkuRatio constructor.
     * @param $ratio
     */
    public function __construct($ratio)
    {
        $this->ratio = $ratio;
    }

    /**
     * Execute the job.
     * @return void
     */
    public function handle()
    {
        $this->exeFillData($this->ratio);
    }

    private function exeFillData($ratio){

        $areaId = SysArea::AREA_EU; //EU站点
        $arr = [];
        $aseKey = [];

        //根据比例，更新EU国家sku预估，同步更新EU国家备货sku表里的预估
        //更新,EU国家sku预估、EU国家备货sku及相关信息
        foreach ($ratio as $_ratio){
            list($countryId,$pSkuId) = explode('_',$_ratio->area_psku);
            if(!array_key_exists($pSkuId,$arr)){
                $_classifyId = ProductSku::getValueById($pSkuId,'classify_id');
                $arr[$pSkuId] = $_classifyId ?: 0;
            }
            $classifyId = $arr[$pSkuId];
            if(!$classifyId) continue;

            //因为对应的国家sku的比例有更新，所以找到对应的国家品类预估用于修改国家sku的预估
            $key = $countryId.'_'.$classifyId;
            if(!in_array($key,$aseKey)){
                $aseKey[] = $key;
            }
        }

        foreach ($aseKey as $_aseKey){
            list($countryId,$classifyId) = explode('_',$_aseKey);

            //EU国家品类预估
            $countryClassifyEstimate = CountryClassifyEstimate::query()
                ->where('country_id',$countryId)
                ->where('classify_id',$classifyId)
                ->first();
            if(!$countryClassifyEstimate) continue;

            //EU国家备货sku
            $countryStockingSkus = CountryStockingSku::query()
                ->where('country_id',$countryId)
                ->where('classify_id',$classifyId)
                ->get();
            if ($countryStockingSkus) {
                foreach ($countryStockingSkus as $stockingSku){
                    list($euCountryId,$pSkuId) = explode('_',$stockingSku->area_psku);
                    $countryPsku = $euCountryId.'_'.$pSkuId; //EU国家+SKU

                    //EU国家sku销售比例
                    $ratio = CountryStockingSkuRatio::getRatio($countryPsku);

                    //EU国家sku预估 (EU国家sku预估 = EUsku比例 * EU国家品类预估)
                    $skuEstimate = CountrySkuEstimate::query()->firstOrNew(['area_psku' => $countryPsku]);
                    for($i = 0;$i < CountrySkuEstimate::ESTIMATE_NUM;$i++){
                        $num = $countryClassifyEstimate->{'estimate_num'.$i} ?? 0; //赋值
                        $estimateNum = intval(round($num * $ratio));
                        $skuEstimate->{'estimate_num'.$i} = $estimateNum;
                        $stockingSku->{'estimate_num'.$i} = $estimateNum;
                    }
                    $skuEstimate->area_id = $areaId;
                    $skuEstimate->country_id = $euCountryId;
                    $skuEstimate->psku_id = $pSkuId;
                    $skuEstimate->classify_id = $classifyId;
                    $skuEstimate->save();

                    $stockingSku->area_id = $areaId;
                    $stockingSku->country_id = $euCountryId;
                    $stockingSku->psku_id = $pSkuId;
                    $stockingSku->classify_id = $classifyId;
                    $stockingSku->save();
                    $stockingSku->over_stock = CountryStockingSku::getOverStock($stockingSku);
                    $stockingSku->over_stock_value = CountryStockingSku::getOverStockValue($stockingSku->over_stock,$stockingSku->unit_price);
                    $stockingSku->save();
                }
            }
        }
        sleep(1);
    }

}
